using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.IO;

namespace Td.Additional.Log
{
    /// <summary>
    /// Class to write log messages into windows event log.
    /// </summary>
    public class EventLog : ILog
    {
        #region Properties

        /// <summary>
        /// Gets or sets the log level.
        /// </summary>
        /// <value>The log level.</value>
        public LogLevel LogLevel { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [log full qualified method name].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [log full qualified method name]; otherwise, <c>false</c>.
        /// </value>
        public bool LogFullQualifiedMethodName { get; set; }

        /// <summary>
        /// Gets or sets the current session log.
        /// </summary>
        /// <value>The current session log.</value>
        public List<LogEntry> CurrentSessionLog { get; set; }

        /// <summary>
        /// Gets or sets the name of the event source.
        /// </summary>
        /// <value>The name of the event source.</value>
        public String EventSourceName { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EventLog"/> class.
        /// </summary>
        public EventLog()
            : base()
        {
            LogLevel = LogLevel.Info;
            LogFullQualifiedMethodName = false;
            CurrentSessionLog = new List<LogEntry>();
            Assembly assembly = Assembly.GetEntryAssembly();
            EventSourceName = Path.GetFileNameWithoutExtension(assembly.FullName);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EventLog"/> class.
        /// </summary>
        /// <param name="eventSourceName">Name of the event source.</param>
        public EventLog(String eventSourceName)
            : this()
        {
            EventSourceName = eventSourceName;
        }

        #endregion

        #region Private helpers

        /// <summary>
        /// Writes the log entry.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <param name="message">The message.</param>
        private void WriteLogEntry(LogLevel level, String message)
        {
            if (level > LogLevel || LogLevel == LogLevel.None)
                return;
            StackTrace stackTrace = new StackTrace();
            System.Reflection.MethodBase methodBase = null;
            int stackIndex = 2;

            while (stackTrace.FrameCount > stackIndex && typeof(ILog).IsAssignableFrom((methodBase = stackTrace.GetFrame(stackIndex).GetMethod()).ReflectedType))
                stackIndex++;

            string callingMethod = LogFullQualifiedMethodName ? String.Format("{0}.{1}", methodBase.ReflectedType.FullName, methodBase.Name) : methodBase.Name;
            System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
            eventLog.Source = EventSourceName;
            EventLogEntryType eventLogEntryType = EventLogEntryType.Information;
            switch (level)
            {
                case LogLevel.Warn:
                    eventLogEntryType = EventLogEntryType.Warning;
                    break;
                case LogLevel.Error:
                    eventLogEntryType = EventLogEntryType.FailureAudit;
                    break;
                case LogLevel.Critical:
                case LogLevel.Fatal:
                    eventLogEntryType = EventLogEntryType.Error;
                    break;
            }
            eventLog.WriteEntry(message, eventLogEntryType);
        }

        #endregion

        #region ILog members

        /// <summary>
        /// New debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Debug(String message)
        {
            WriteLogEntry(LogLevel.Debug, message);
        }

        /// <summary>
        /// New debug message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Debug(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Debug, String.Format(message, args));
        }

        /// <summary>
        /// New information message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Info(String message)
        {
            WriteLogEntry(LogLevel.Info, message);
        }

        /// <summary>
        /// New information message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Info(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Info, String.Format(message, args));
        }

        /// <summary>
        /// New warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Warn(String message)
        {
            WriteLogEntry(LogLevel.Warn, message);
        }

        /// <summary>
        /// New warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Warn(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Warn, String.Format(message, args));
        }

        /// <summary>
        /// New error message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Error(String message)
        {
            WriteLogEntry(LogLevel.Error, message);
        }

        /// <summary>
        /// New error message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Error(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Error, String.Format(message, args));
        }

        /// <summary>
        /// New critical message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Critical(String message)
        {
            WriteLogEntry(LogLevel.Critical, message);
        }

        /// <summary>
        /// New critical message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Critical(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Critical, String.Format(message, args));
        }

        /// <summary>
        /// New fatal message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Fatal(String message)
        {
            WriteLogEntry(LogLevel.Fatal, message);
        }

        /// <summary>
        /// New fatal message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        public void Fatal(String message, params object[] args)
        {
            WriteLogEntry(LogLevel.Fatal, String.Format(message, args));
        }

        #endregion
    }
}